<template>
  <div class="tech-login-wrap">
    <!-- 动态背景 -->
    <div class="tech-bg">
      <div class="particles-container">
        <div class="particle" v-for="i in 50" :key="i" :style="getParticleStyle(i)"></div>
      </div>
      <div class="grid-overlay"></div>
      <div class="glow-orbs">
        <div class="orb orb-1"></div>
        <div class="orb orb-2"></div>
        <div class="orb orb-3"></div>
      </div>
    </div>

    <div class="login-container">
      <div class="tech-login-card">

        <!-- 头部区域 -->
        <div class="tech-header">
          <div class="logo-container">
            <div class="logo-glow">
              <div class="logo-icon">
                <svg viewBox="0 0 100 100" class="tech-logo">
                  <defs>
                    <linearGradient id="logoGradient" x1="0%" y1="0%" x2="100%" y2="100%">
                      <stop offset="0%" style="stop-color:#00f5ff"/>
                      <stop offset="100%" style="stop-color:#ff00ff"/>
                    </linearGradient>
                  </defs>
                  <circle cx="50" cy="50" r="45" fill="none" stroke="url(#logoGradient)" stroke-width="2"/>
                  <circle cx="50" cy="50" r="30" fill="none" stroke="url(#logoGradient)" stroke-width="1"/>
                  <circle cx="50" cy="50" r="15" fill="url(#logoGradient)"/>
                </svg>
              </div>
            </div>
            <h1 class="system-title">
              <span class="title-text">ERP</span>
              <span class="title-sub">管理系统</span>
            </h1>
          </div>
          <div class="status-indicator">
            <div class="status-dot"></div>
            <span>系统在线</span>
          </div>
        </div>

        <!-- 主体内容 -->
        <div class="tech-body">
          <div class="welcome-section">
            <h2 class="welcome-title">
              <span class="glitch" data-text="WELCOME BACK">WELCOME BACK</span>
            </h2>
            <p class="welcome-subtitle">请验证您的身份以继续访问</p>
          </div>

          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="tech-form"
            @submit.prevent="handleLogin"
          >
            <el-form-item prop="username">
              <div class="tech-input-wrapper">
                <label class="input-label">用户名</label>
                <el-input
                  v-model="loginForm.username"
                  placeholder="请输入用户名"
                  size="large"
                  class="tech-input"
                >
                  <template #prefix>
                    <i class="tech-icon user-icon"></i>
                  </template>
                </el-input>
                <div class="input-border"></div>
              </div>
            </el-form-item>

            <el-form-item prop="password">
              <div class="tech-input-wrapper">
                <label class="input-label">密码</label>
                <el-input
                  v-model="loginForm.password"
                  type="password"
                  placeholder="请输入密码"
                  size="large"
                  show-password
                  @keyup.enter="handleLogin"
                  class="tech-input"
                >
                  <template #prefix>
                    <i class="tech-icon lock-icon"></i>
                  </template>
                </el-input>
                <div class="input-border"></div>
              </div>
            </el-form-item>

            <el-form-item>
              <div class="form-options">
                <label class="tech-checkbox">
                  <input type="checkbox" v-model="loginForm.rememberMe">
                  <span class="checkmark"></span>
                  <span class="checkbox-text">记住登录状态</span>
                </label>
                <a href="#" @click.prevent="showForgotPassword" class="forgot-link">
                  忘记密码？
                </a>
              </div>
            </el-form-item>

            <el-form-item>
              <button
                type="button"
                class="tech-submit-btn"
                :class="{ loading: loading }"
                @click="handleLogin"
                :disabled="loading"
              >
                <span class="btn-bg"></span>
                <span class="btn-text">
                  <span v-if="loading" class="loading-text">
                    <i class="loading-spinner"></i>
                    正在验证...
                  </span>
                  <span v-else>登录系统</span>
                </span>
                <div class="btn-particles">
                  <div class="btn-particle" v-for="i in 6" :key="i"></div>
                </div>
              </button>
            </el-form-item>
          </el-form>
        </div>

        <!-- 底部信息 -->
        <div class="tech-footer">
          <div class="demo-info">
            <div class="info-line">
              <span class="info-label">演示账号:</span>
              <span class="info-value">admin</span>
            </div>
            <div class="info-line">
              <span class="info-label">演示密码:</span>
              <span class="info-value">123456</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 忘记密码对话框 -->
    <ForgotPasswordDialog
      v-model="forgotPasswordVisible"
      @success="handleResetSuccess"
    />
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import ForgotPasswordDialog from './components/ForgotPasswordDialog.vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const emit = defineEmits(['login'])

const loginFormRef = ref()
const loading = ref(false)
const forgotPasswordVisible = ref(false)

const loginForm = reactive({
  username: 'admin',
  password: '123456',
  rememberMe: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

// 生成粒子样式
const getParticleStyle = (index) => {
  const size = Math.random() * 4 + 1
  const x = Math.random() * 100
  const y = Math.random() * 100
  const duration = Math.random() * 20 + 10
  const delay = Math.random() * 5
  
  return {
    width: `${size}px`,
    height: `${size}px`,
    left: `${x}%`,
    top: `${y}%`,
    animationDuration: `${duration}s`,
    animationDelay: `${delay}s`
  }
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    await loginFormRef.value.validate()
    loading.value = true

    const response = await userStore.login(loginForm)

    if (response.code === 200) {
      ElMessage.success(response.msg || '登录成功')
      emit('login', response)
      
      const redirect = route.query.redirect
      if (redirect && redirect !== '/login') {
        router.push(decodeURIComponent(redirect))
      } else {
        router.push('/dashboard')
      }
    } else {
      ElMessage.error(response.msg || '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.message || '网络请求失败，请重试')
  } finally {
    loading.value = false
  }
}

const showForgotPassword = () => {
  forgotPasswordVisible.value = true
}

const handleResetSuccess = () => {
  forgotPasswordVisible.value = false
}
</script>

<style scoped>
/* 主容器 */
.tech-login-wrap {
  position: relative;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, #0a0a0a 0%, #000000 100%);
  padding: 20px;
  overflow: hidden;
}

/* 科技背景 */
.tech-bg {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

/* 粒子容器 */
.particles-container {
  position: absolute;
  inset: 0;
}

.particle {
  position: absolute;
  background: #00f5ff;
  border-radius: 50%;
  opacity: 0.6;
  animation: float infinite linear;
  box-shadow: 0 0 6px #00f5ff;
}

@keyframes float {
  0% { transform: translateY(100vh) rotate(0deg); opacity: 0; }
  10% { opacity: 0.6; }
  90% { opacity: 0.6; }
  100% { transform: translateY(-100px) rotate(360deg); opacity: 0; }
}

/* 网格覆盖层 */
.grid-overlay {
  position: absolute;
  inset: 0;
  background-image: 
    linear-gradient(rgba(0, 245, 255, 0.1) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 245, 255, 0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: gridMove 20s linear infinite;
}

@keyframes gridMove {
  0% { transform: translate(0, 0); }
  100% { transform: translate(50px, 50px); }
}

/* 发光球体 */
.glow-orbs {
  position: absolute;
  inset: 0;
}

.orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(40px);
  animation: orbFloat 8s ease-in-out infinite;
}

.orb-1 {
  width: 200px;
  height: 200px;
  background: radial-gradient(circle, #00f5ff 0%, transparent 70%);
  top: 20%;
  left: 10%;
  animation-delay: 0s;
}

.orb-2 {
  width: 150px;
  height: 150px;
  background: radial-gradient(circle, #ff00ff 0%, transparent 70%);
  top: 60%;
  right: 15%;
  animation-delay: 2s;
}

.orb-3 {
  width: 100px;
  height: 100px;
  background: radial-gradient(circle, #00ff88 0%, transparent 70%);
  bottom: 30%;
  left: 60%;
  animation-delay: 4s;
}

@keyframes orbFloat {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(30px, -30px) scale(1.1); }
}

/* 登录容器 */
.login-container {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 480px;
}

/* 科技登录卡片 */
.tech-login-card {
  position: relative;
  background: rgba(10, 10, 10, 0.8);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(0, 245, 255, 0.2);
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 
    0 0 50px rgba(0, 245, 255, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

/* 科技边框 */
.card-border {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.border-corner {
  position: absolute;
  width: 20px;
  height: 20px;
  border: 2px solid #00f5ff;
}

.corner-tl {
  top: 10px;
  left: 10px;
  border-right: none;
  border-bottom: none;
  animation: cornerGlow 2s ease-in-out infinite alternate;
}

.corner-tr {
  top: 10px;
  right: 10px;
  border-left: none;
  border-bottom: none;
  animation: cornerGlow 2s ease-in-out infinite alternate 0.5s;
}

.corner-bl {
  bottom: 10px;
  left: 10px;
  border-right: none;
  border-top: none;
  animation: cornerGlow 2s ease-in-out infinite alternate 1s;
}

.corner-br {
  bottom: 10px;
  right: 10px;
  border-left: none;
  border-top: none;
  animation: cornerGlow 2s ease-in-out infinite alternate 1.5s;
}

@keyframes cornerGlow {
  0% { box-shadow: 0 0 5px #00f5ff; }
  100% { box-shadow: 0 0 20px #00f5ff, 0 0 30px #00f5ff; }
}

/* 头部区域 */
.tech-header {
  padding: 16px 16px 8px;
  text-align: center;
  position: relative;
}

.logo-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}

.logo-glow {
  position: relative;
}

.logo-icon {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tech-logo {
  width: 100%;
  height: 100%;
  animation: logoRotate 10s linear infinite;
  filter: drop-shadow(0 0 10px #00f5ff);
}

@keyframes logoRotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.system-title {
  margin: 0;
  text-align: center;
}

.title-text {
  display: block;
  font-size: 22px;
  font-weight: 700;
  background: linear-gradient(45deg, #00f5ff, #ff00ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 0 0 30px rgba(0, 245, 255, 0.5);
}

.title-sub {
  display: block;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
  font-weight: 300;
  margin-top: 5px;
}

.status-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #00ff88;
  font-size: 10px;
}

.status-dot {
  width: 6px;
  height: 6px;
  background: #00ff88;
  border-radius: 50%;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; box-shadow: 0 0 5px #00ff88; }
  50% { opacity: 0.5; box-shadow: 0 0 20px #00ff88; }
}

/* 主体内容 */
.tech-body {
  padding: 0 16px 16px;
}

.welcome-section {
  text-align: center;
  margin-bottom: 12px;
}

.welcome-title {
  margin: 0 0 10px;
  font-size: 18px;
  font-weight: 600;
}

.glitch {
  position: relative;
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  text-transform: uppercase;
  animation: glitch 2s infinite;
}

.glitch::before,
.glitch::after {
  content: attr(data-text);
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.glitch::before {
  animation: glitch-1 0.5s infinite;
  color: #00f5ff;
  z-index: -1;
}

.glitch::after {
  animation: glitch-2 0.5s infinite;
  color: #ff00ff;
  z-index: -2;
}

@keyframes glitch {
  0%, 100% { transform: translate(0); }
  20% { transform: translate(-2px, 2px); }
  40% { transform: translate(-2px, -2px); }
  60% { transform: translate(2px, 2px); }
  80% { transform: translate(2px, -2px); }
}

@keyframes glitch-1 {
  0%, 100% { transform: translate(0); }
  20% { transform: translate(2px, -2px); }
  40% { transform: translate(-2px, 2px); }
  60% { transform: translate(-2px, -2px); }
  80% { transform: translate(2px, 2px); }
}

@keyframes glitch-2 {
  0%, 100% { transform: translate(0); }
  20% { transform: translate(-2px, 2px); }
  40% { transform: translate(2px, -2px); }
  60% { transform: translate(2px, 2px); }
  80% { transform: translate(-2px, -2px); }
}

.welcome-subtitle {
  margin: 0;
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
}

/* 科技表单 */
.tech-form {
  margin-top: 12px;
}

.tech-input-wrapper {
  position: relative;
  margin-bottom: 12px;
  width: 100%;
}

.tech-form :deep(.el-form-item) {
  margin-bottom: 12px;
}

.input-label {
  display: block;
  color: #00f5ff;
  font-size: 12px;
  font-weight: 500;
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.tech-input :deep(.el-input__wrapper) {
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(0, 245, 255, 0.3);
  border-radius: 8px;
  padding: 10px 14px;
  transition: all 0.3s ease;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
  width: 100%;
  box-sizing: border-box;
}

.tech-input :deep(.el-input) {
  width: 100%;
}

.tech-input :deep(.el-input__prefix),
.tech-input :deep(.el-input__suffix) {
  min-width: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tech-input :deep(.el-input__suffix-inner) {
  display: flex;
  align-items: center;
}

.tech-input :deep(.el-input__wrapper):hover {
  border-color: rgba(0, 245, 255, 0.6);
  box-shadow: 
    inset 0 2px 4px rgba(0, 0, 0, 0.3),
    0 0 15px rgba(0, 245, 255, 0.2);
}

.tech-input :deep(.el-input__wrapper.is-focus) {
  border-color: #00f5ff;
  box-shadow: 
    inset 0 2px 4px rgba(0, 0, 0, 0.3),
    0 0 20px rgba(0, 245, 255, 0.4);
}

.tech-input :deep(.el-input__inner) {
  color: #fff;
  background: transparent;
  font-size: 16px;
}

.tech-input :deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.4);
}

.tech-icon {
  width: 16px;
  height: 16px;
  margin-right: 0;
}

.user-icon::before {
  content: "👤";
  color: #00f5ff;
}

.lock-icon::before {
  content: "🔒";
  color: #00f5ff;
}

.input-border {
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(90deg, #00f5ff, #ff00ff);
  transition: width 0.3s ease;
}

.tech-input:focus-within + .input-border {
  width: 100%;
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 12px 0;
}

.tech-checkbox {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.7);
  font-size: 14px;
}

.tech-checkbox input[type="checkbox"] {
  display: none;
}

.checkmark {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(0, 245, 255, 0.5);
  border-radius: 3px;
  margin-right: 10px;
  position: relative;
  transition: all 0.3s ease;
}

.tech-checkbox input[type="checkbox"]:checked + .checkmark {
  background: #00f5ff;
  border-color: #00f5ff;
  box-shadow: 0 0 10px rgba(0, 245, 255, 0.5);
}

.tech-checkbox input[type="checkbox"]:checked + .checkmark::after {
  content: "✓";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #000;
  font-size: 12px;
  font-weight: bold;
}

.forgot-link {
  color: #00f5ff;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s ease;
}

.forgot-link:hover {
  color: #ff00ff;
  text-shadow: 0 0 5px #ff00ff;
}

/* 科技提交按钮 */
.tech-submit-btn {
  position: relative;
  width: 100%;
  height: 40px;
  background: transparent;
  border: 2px solid #00f5ff;
  border-radius: 8px;
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  overflow: hidden;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.btn-bg {
  position: absolute;
  inset: 0;
  background: linear-gradient(45deg, transparent, rgba(0, 245, 255, 0.1), transparent);
  transform: translateX(-100%);
  transition: transform 0.6s ease;
}

.tech-submit-btn:hover .btn-bg {
  transform: translateX(100%);
}

.tech-submit-btn:hover {
  box-shadow: 
    0 0 20px rgba(0, 245, 255, 0.5),
    inset 0 0 20px rgba(0, 245, 255, 0.1);
  border-color: #ff00ff;
}

.btn-text {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid #00f5ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.btn-particles {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.btn-particle {
  position: absolute;
  width: 4px;
  height: 4px;
  background: #00f5ff;
  border-radius: 50%;
  opacity: 0;
}

.tech-submit-btn:hover .btn-particle {
  animation: particleExplode 0.6s ease-out;
}

.btn-particle:nth-child(1) { top: 20%; left: 20%; animation-delay: 0s; }
.btn-particle:nth-child(2) { top: 20%; right: 20%; animation-delay: 0.1s; }
.btn-particle:nth-child(3) { bottom: 20%; left: 20%; animation-delay: 0.2s; }
.btn-particle:nth-child(4) { bottom: 20%; right: 20%; animation-delay: 0.3s; }
.btn-particle:nth-child(5) { top: 50%; left: 10%; animation-delay: 0.4s; }
.btn-particle:nth-child(6) { top: 50%; right: 10%; animation-delay: 0.5s; }

@keyframes particleExplode {
  0% { opacity: 1; transform: scale(0); }
  50% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(0); }
}

.tech-submit-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

/* 底部信息 */
.tech-footer {
  padding: 8px 16px;
  border-top: 1px solid rgba(0, 245, 255, 0.2);
  background: rgba(0, 0, 0, 0.3);
}

.demo-info {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.info-line {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.info-label {
  color: rgba(255, 255, 255, 0.5);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.info-value {
  color: #00f5ff;
  font-size: 14px;
  font-weight: 600;
  font-family: 'Courier New', monospace;
  padding: 4px 8px;
  background: rgba(0, 245, 255, 0.1);
  border: 1px solid rgba(0, 245, 255, 0.3);
  border-radius: 4px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .tech-login-wrap {
    padding: 10px;
  }
  
  .login-container {
    max-width: 100%;
  }
  
  .tech-header,
  .tech-body,
  .tech-footer {
    padding-left: 12px;
    padding-right: 12px;
  }
  
  .title-text {
    font-size: 20px;
  }
  
  .demo-info {
    flex-direction: column;
    gap: 8px;
  }
}
</style>

